package com.nevermore.floatbar

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = MyAdapter()

        val floatRvItemContainer = findViewById<FloatRvItemContainer>(R.id.floatItemContainer)
        floatRvItemContainer.showItemFloat = {
            it % 8 == 0
        }
        floatRvItemContainer.observe()
    }


    inner class MyAdapter : RecyclerView.Adapter<MyVH>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyVH {

            val view = LayoutInflater.from(parent.context)
                .inflate(android.R.layout.simple_list_item_1, parent, false)
            return MyVH(view)
        }

        override fun onBindViewHolder(holder: MyVH, position: Int) {
            val itemViewType = getItemViewType(position)
            holder.textView.text = "position:$position"
            holder.textView.setBackgroundColor(if (itemViewType == 0) Color.BLUE else Color.WHITE)
            holder.textView.setTextColor(if (itemViewType == 0) Color.WHITE else Color.GRAY)
        }

        override fun getItemViewType(position: Int): Int {
            return if (position % 8 == 0) 0 else 1
        }

        override fun getItemCount(): Int {
            return 72
        }

    }

    inner class MyVH constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView = itemView as TextView

    }
}